import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-10-26
 * Time:9:56
 */
public class TestDemo {
    public static void main5(String[] args) {
        Random random = new Random();
        int[] array = new int[10];
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(5);
        }
        System.out.println(Arrays.toString(array));
        Set<Integer> set = new HashSet<>();
        Map<Integer,Integer> map = new HashMap<>();

        for (int i = 0; i < array.length; i++) {
            if (map.containsKey(array[i])){
                int k = map.get(array[i]);
                map.put(array[i],k + 1);
            }else {
                map.put(array[i], 1);
            }
        }
        System.out.println(map);
    }
    public static void main4(String[] args) {
        Random random = new Random();
        int[] array = new int[10];
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(5);
        }
        System.out.println(Arrays.toString(array));
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < array.length; i++) {
            set.add(array[i]);
        }
        System.out.println(set);
    }
    public static void main3(String[] args) {
        Random random = new Random();
        int[] array = new int[10];
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(5);
        }
        System.out.println(Arrays.toString(array));
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < array.length; i++) {
            if (set.contains(array[i])){
                System.out.println(array[i]);
                break;
            }
            set.add(array[i]);
        }
    }
    /**
     * set中的元素不能重复
     * 是无序的
     */
    //找找迭代器相关的资料  重点研究删除
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        HashSet<Integer> set1 = new HashSet<>();
        set1.add(1);
        set1.add(2);
        set1.add(3);
        set1.add(4);
        System.out.println(set1);
        System.out.println("==========================");
        set.add("hello1");
        set.add("hello2");
        set.add("hello3");
        set.add("hello4");
        set.add("hello5");
        System.out.println(set);

        //迭代器 不可以变量map
        Iterator<String> it= set.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
    }
    /**
     * 1.往map中存储数据的时候，会根据key做出一系列运算才会放到hashmap中
     * 2.如果key一样，那么会覆盖原来的value值
     * 3.key 和 value是允许位null的
     * @param args
     */
    public static void main1(String[] args) {
        Map<String,String> map = new HashMap<>();
        HashMap<String,String> map2 = new HashMap<>();

        //put函数
        map.put("猴哥","孙悟空");
//        map.put("null","孙悟空");
        map.put("八戒","猪悟能");
        map.put("沙僧","沙悟净");
//        map.put("猴哥","沙悟净");
        System.out.println(map);

        //打印map的方式
        Set<Map.Entry<String,String>> set = map.entrySet();
        for (Map.Entry<String,String> entry:
             set) {
            System.out.println("key : " + entry.getKey() + "  value:" +entry.getValue() );
        }

        String str = map.get("八戒");
        System.out.println(str);//没有就是null
        String str1 = map.getOrDefault("八戒2","gaobo");
        System.out.println(str1);


    }
}
